package com.chookss.video

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.chookss.R
import com.chookss.tools.MyEvent
import com.chookss.tools.Utils
import com.lvgroup.hospital.base.BaseAct
import kotlinx.android.synthetic.main.activity_answer_card.common_title_back
import kotlinx.android.synthetic.main.activity_video_search.*
import kotlinx.android.synthetic.main.common_title.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/26 17:09
 * Note:
 */
class VideoSearchActivity : BaseAct() {
    var searchFragment = VideoSearchFragment()
    private var videoType = 0 //2发布图文过来的0默认

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video_search)
        videoType= intent?.getIntExtra("type",0)!!
        common_title_txt.text="搜索"
        common_title_back.setOnClickListener { onBackPressed() }
        supportFragmentManager.beginTransaction().replace(R.id.flSearch, searchFragment).commitAllowingStateLoss()
        et_search.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }
            override fun afterTextChanged(s: Editable) {
                searchFragment.setContent(s.toString())
                if (Utils.isNull(s.toString())) {
                    search_cancle.setText("取消")
                } else {
                    search_cancle.setText("搜索")
                }
            }
        })
        search_cancle.setOnClickListener(View.OnClickListener {
            if ("搜索" == search_cancle.getText().toString()) {
                searchFragment.toSearch(et_search.getText().toString())
            } else {
                onBackPressed()
            }
        })
        et_search.setOnKeyListener(searchFragment.onKeyListener)
        searchFragment.setType(videoType)
        viewPager.adapter = MyAdapter(supportFragmentManager, arrayOf(
            VideoRecommendsFragment1(), VideoRecommendsFragment2(), VideoRecommendsFragment3(),
        ), arrayOf("精选", "热点", "明星"))
        viewPager.offscreenPageLimit = 3
        tabLayout.setupWithViewPager(viewPager)
        initState()
    }

    override fun onRestart() {
        super.onRestart()

    }
    override fun initData() {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onMoonEvent(messageEvent: MyEvent) {
        //事件订阅者处理事件
        if (messageEvent.hashMap != null) {
            val map: Map<String, String?> = messageEvent.hashMap
              if(map.containsKey("Video_search_key")){
                  if(searchFragment!=null){
                      searchFragment.addSearch(map.get("Video_search_key").toString())
                  }
              }
        } else {
            val msg = messageEvent.message
            if(msg.equals("Video_search_finish")){
                finish()
            }
        }
    }
    class MyAdapter(fm : FragmentManager, var fragments:Array<Fragment>, var titles:Array<String>) : FragmentPagerAdapter(fm) {
        override fun getCount() = fragments.size

        override fun getItem(position: Int) = fragments[position]

        override fun getPageTitle(position: Int) = titles[position]

    }
}